﻿using blackhouse.Translator.Business.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace blackhouse.Translator.Business {
    public class TranslatorResourcesManager {

        #region Fields

        private TranslatorConfigurationSection config;
        private ITranslatorResourceManager manager;

        #endregion

        #region Constructors

        public TranslatorResourcesManager() {
            this.config = TranslatorConfigurationSection.Get();
            this.manager = this.GetComputedResourceManager();
        }

        #endregion

        #region Methods

        private ITranslatorResourceManager GetComputedResourceManager() {
            ComputedTranslatorResourceManager manager = new ComputedTranslatorResourceManager();
            foreach (TranslatorConfigurationManagerElement configManager in this.config.ResourceManagers) {
                ITranslatorResourceManager setManager = this.GetManagerFromConfig(configManager);
                if (setManager != null) manager.Add(setManager);
            }
            return manager;
        }

        private ITranslatorResourceManager GetManagerFromConfig(TranslatorConfigurationManagerElement managerConfig) {
            Type t = Type.GetType(managerConfig.Type, false);
            if (t == null) return null;
            ITranslatorResourceManager manager = Activator.CreateInstance(t) as ITranslatorResourceManager;
            
            return manager;
        }

        public ITranslatorResource[] GetResources() {
            return this.manager.GetResources();
        }

        #endregion

    }
}
